<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 *
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel
 */
class Config extends ParameterHolder
{

  public function __construct($app_dir)
  {
    parent::__construct();
    
    $this->set('app_dir', sy_fix_path($app_dir));
    
    $this->load('sy:/config/settings-yaml.php');
    
    /**
     * Absolute path to the web root
     */
    $this->set('www_dir', sy_fix_path(dirname($_SERVER['SCRIPT_FILENAME'])));
    
    /**
     * Absolute path to public files
     */
    $this->set('files_dir', 'www:/files');
    
    /**
     * Base URL and protocol
     */
    $secure = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
    $this->set('url_protocol', substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $secure);
    $this->set('base_url', $this->get('url_protocol') . '://' . $_SERVER['HTTP_HOST']);
    $this->set('app_url', $this->get('base_url') . dirname($_SERVER['SCRIPT_NAME']));
    
    Language::$config = $this;
  }

  public function applyFilters(&$value)
  {
    if (is_string($value)) {
      $search = array(
        '#^app:#', '#^sy:#', '#^www:#' 
      );
      
      $replace = array(
        $this->get('app_dir'), SY_DIR, $this->get('www_dir') 
      );
      
      $value = preg_replace($search, $replace, $value);
    }
    
    return $value;
  }

  public function load($file)
  {
    $this->applyFilters($file);
    
    $data = spyc_load_file($file);
    
    $this->copyAll($data);
    
    return $this;
  }

  protected function setRaw($name, $value)
  {
    $keys = explode(':', $name);
    
    $data = & $this->data;
    
    $i = 0;
    $c = count($keys);
    
    while ($i < $c - 1) {
      $key = $keys[$i];
      
      if (! isset($data[$key]))
        $data[$key] = array();
      
      $data = & $data[$key];
      
      $i ++;
    }
    
    if (is_array($value)) {
      array_walk_recursive($value, array(
        $this, 'applyFilters' 
      ));
    }
    elseif (is_string($value)) {
      $this->applyFilters($value);
    }
    
    $data[$keys[$i]] = $value;
  }

  protected function hasRaw($name)
  {
    $keys = explode(':', $name);
    
    $data = & $this->data;
    
    $i = 0;
    $c = count($keys);
    
    while ($i < $c) {
      $key = $keys[$i];
      
      if (! isset($data[$key]))
        break;
      
      $data = & $data[$key];
      
      $i ++;
    }
    
    return $i == $c;
  }

  protected function getRaw($name)
  {
    $keys = explode(':', $name);
    
    $data = & $this->data;
    
    $i = 0;
    $c = count($keys);
    
    while ($i < $c - 1) {
      $key = $keys[$i];
      
      if (! isset($data[$key]))
        return null;
      
      $data = & $data[$key];
      
      $i ++;
    }
    
    return $data[$keys[$i]];
  }

  protected function delRaw($name)
  {
    $keys = explode(':', $name);
    
    $data = & $this->data;
    
    $i = 0;
    $c = count($keys);
    
    while ($i < $c - 1) {
      $key = $keys[$i];
      
      if (! isset($data[$key]))
        return false;
      
      $data = & $data[$key];
      
      $i ++;
    }
    
    unset($data[$keys[$i]]);
  }

}

?>